#! /bin/bash
#! git clone $url_path --recurse-submodules
#提示“Permission denied” ,是因为权限不够，增加权限，在命令终端里输入：chmod +x newVersionAll.sh回车
#! 请在 git_framework_cfg.cfg 文件中配置依赖的 git 仓库。
#! 格式：git_url branch_name module_name 注意空格，每行一条完整的git仓库信息，三个字段。
echo "============================================================="
echo " ✅ ✅ ✅ --start-git-install-all-- ✅ ✅ ✅  "

GitSuffix=".git"

COMMAND_DIR=$PWD
echo "命令执行位置 ${COMMAND_DIR}"

TARGET_DIR=$(cd $(dirname $0); pwd)
echo "项目目录位置 ${TARGET_DIR}"

echo "进入项目目录 cd ${TARGET_DIR}"
cd "${TARGET_DIR}"

ParentNameSpace=$(basename $(dirname $PWD))
echo "项目空间名称 $ParentNameSpace"

function git_install_all() {
  echo " ✅ ✅ ✅ --${ModulesNameSpace}-start-- ✅ ✅ ✅  "
  echo " ✅ ✅ ✅ --${GitCfg}-start-- ✅ ✅ ✅  "

#  reset params
  i=0
  unset GitUrl
  unset BranchName
  unset ProjectName

  while read GitUrl[i] BranchName[i] ProjectName[i]
  do
    let "i++"
  done <${GitCfg}

  if [[ -z "${GitUrl[0]}" ]]
  then
    echo " ⚠️⚠️⚠️ ${GitCfg} 未配置 ⚠️⚠️⚠️ "
    echo " ✅ ✅ ✅ --${ModulesNameSpace}-end-- ✅ ✅ ✅  "
    return
  fi

  # if [ -d ${ModulesNameSpace} ]
  # then
  #   echo " ⚠️⚠️⚠️ ${ModulesNameSpace} exists ⚠️⚠️⚠️️ "
  #   chmod 777 ${ModulesNameSpace}
  # else
  #   mkdir ${ModulesNameSpace}
  #   chmod 777 ${ModulesNameSpace}
  #   echo " ✅ mkdir ${ModulesNameSpace} ✅ "
  # fi
  # cd ${ModulesNameSpace}

  PartNum=${#GitUrl[@]}
  for ((i=0;i<$PartNum;i++))
  do
    echo "============================================================="
    echo "😊😊😊-- ${ModulesNameSpace} ${ProjectName[$i]} repo start --😊😊😊"
    echo "url:${GitUrl[i]}"
    echo "branch:${BranchName[$i]}"
    echo "name:${ProjectName[$i]}"
    if [[ ${GitUrl[$i]} == *${GitSuffix} ]]
    then
      echo "git clone ${GitUrl[i]} -b ${BranchName[$i]} ${ProjectName[$i]}"
      git clone ${GitUrl[i]} -b ${BranchName[$i]} ${ProjectName[$i]}
      echo "cd ${ProjectName[$i]}"
      cd ${ProjectName[$i]}
      pwd
      echo "git checkout ${BranchName[$i]}"
      git checkout ${BranchName[$i]}
      echo "git pull"
      git pull
      echo "cd .."
      cd ..
    else
      echo " ❌ ❌ ❌ url:${GitUrl[i]} branch:${BranchName[$i]} name:${ProjectName[$i]} ❌ ❌ ❌  "
    fi
  done
  cd ..

  echo " ✅ ✅ ✅ --${ModulesNameSpace}-end-- ✅ ✅ ✅  "
}

function git_install() {
  echo " ✅ ✅ ✅ --${GitCfg}-start-- ✅ ✅ ✅  "

  echo "cd ${COMMAND_DIR}"
  cd "${COMMAND_DIR}"
  pwd

#  reset params
  i=0
  unset GitUrl
  unset BranchName
  unset ProjectName

  while read GitUrl[i] BranchName[i] ProjectName[i]
  do
    let "i++"
  done <${GitCfg}

  if [[ -z "${GitUrl[0]}" ]]
  then
    echo " ⚠️⚠️⚠️ ${GitCfg} 未配置 -end-- ⚠️⚠️⚠️ "
    echo " ✅ ✅ ✅ --${GitCfg}-end-- ✅ ✅ ✅  "
    return
  fi

  PartNum=${#GitUrl[@]}
  for ((i=0;i<$PartNum;i++))
  do
    echo "============================================================="
    echo "😊😊😊-- ${ProjectName[$i]} repo start --😊😊😊"
    echo "url:${GitUrl[i]}"
    echo "branch:${BranchName[$i]}"
    echo "name:${ProjectName[$i]}"
    if [[ ${GitUrl[$i]} == *${GitSuffix} ]]
    then

      if [ -d ${ProjectName[$i]} ]
      then
        echo " ⚠️⚠️⚠️ ${ProjectName[$i]} exists ⚠️⚠️⚠️️ "
      else
        echo "git clone ${GitUrl[i]} -b ${BranchName[$i]} ${ProjectName[$i]}"
        git clone ${GitUrl[i]} -b ${BranchName[$i]} ${ProjectName[$i]}
      fi

      echo "cd ${ProjectName[$i]}"
      cd ${ProjectName[$i]}
      pwd
      echo "git checkout ${BranchName[$i]}"
      git checkout ${BranchName[$i]}
      echo "git pull"
      git pull
      echo "cd .."
      cd ..
    else
      echo " ❌ ❌ ❌ url:${GitUrl[i]} branch:${BranchName[$i]} name:${ProjectName[$i]} ❌ ❌ ❌  "
    fi
  done

  echo " ✅ ✅ ✅ --${GitCfg}-end-- ✅ ✅ ✅  "
}

GitCfg=git_component_cfg.cfg
ModulesNameSpace=component
git_install $PWD

echo "返回执行位置 cd ${COMMAND_DIR}"
cd "${COMMAND_DIR}"

echo " ✅ ✅ ✅ --finished-git-install-all-- ✅ ✅ ✅  "
echo "============================================================="
